"""
多行注释：
第1页  https://tieba.baidu.com/f?kw=ycy&ie=utf-8&pn=0
第2页  https://tieba.baidu.com/f?kw=ycy&ie=utf-8&pn=50
第3页  https://tieba.baidu.com/f?kw=ycy&ie=utf-8&pn=100
第4页  https://tieba.baidu.com/f?kw=ycy&ie=utf-8&pn=150
第n页  pn = (n-1) * 50
"""
import requests


class Tieba:
    def __init__(self, page):
        # 爬取的页面个数
        self.page = page
        self.url = "https://tieba.baidu.com/f"
        self.run()

    def run(self):
        i = 0   # i 从0-4
        while i < self.page:
            payload = {
                "kw": "ycy",
                "ie": "utf-8",
                "pn": i * 50
            }
            headers = {
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                              'Chrome/97.0.4692.71 Safari/537.36',
            }
            response = requests.get(self.url, params=payload, headers=headers)
            html = response.content.decode(encoding="utf-8")
            # print(html)

            with open(f"./sina/{i+1}-ycy.html", "w", encoding="utf-8") as f:
                f.write(html)

            i += 1
            print("=>" * i * 4 + "--" * (self.page - i) * 4 + f" {(i/self.page)*100}%")


if __name__ == '__main__':
    Tieba(5)


